#include "Utility.h"

namespace util
{

int stricmp(char *s, char *t)
{
	char c, d;
    while (*s || *t)
    {
		if (!*s) 
			return -1;
        if (!*t)
			return -1;

		c = tolower(*s++);
		d = tolower(*t++);

		if (c < d)
			return -1;
		if (c > d)
			return -1;
	}
	return 0;
}


double string_to_double(std::string s)
{
	std::istringstream instr(s);
	double x;
	instr >> x;
	return x;
}

int string_to_int(std::string s)
{
	std::istringstream instr(s);
	int x;
	instr >> x;
	return x;
}

std::string remove_leading_blanks(std::string string_var)                // function to remove leading blanks in strings
{
	do
	{
		if (string_var.substr(0,1) == " ")
			string_var = string_var.substr(1,string_var.length() - 1);
	} while (string_var.substr(0,1) == " ");
	return string_var;
}

std::string string_print_spacer(std::string string_var, unsigned int width_field)    // function to put spaces in std::string output fields
{
	std::string spacer = "";
	if (width_field < string_var.length())  
	{
		std::cout << string_var << " longer than "  << width_field << " wide";
		std::cin.get();
		return("0");                                                
	}
	else    
	{
	for (unsigned int i = 1; i <= (width_field - string_var.length()); ++i)
		spacer += " ";
	}
	return spacer;
}

double great_circle(double dep_lat, double dep_lon, double des_lat, double des_lon)
{
	double distance;
	double delta_lat;
	double delta_lon;
	double a;
	double b;
	double c;
	double s;
	double angle;
	double deg_to_rad = 3.1415927 / 180.0;
	double rad_to_deg = 180.0 / 3.1415927;

	delta_lat = fabs(dep_lat - des_lat);            
	delta_lon = fabs(dep_lon - des_lon);
	a = 2.0 * sin(.5 * delta_lat * deg_to_rad);                                
	b = 2.0 * sin(.5 * delta_lon * deg_to_rad) * cos(dep_lat * deg_to_rad);
	c = 2.0 * sin(.5 * delta_lon * deg_to_rad) * cos(des_lat * deg_to_rad);
	s = sqrt(a * a + (b * c));
	angle = 2.0 * (asin(.5 * s) * rad_to_deg);
	distance =(angle / 360.0) * 21600.0;
	return distance;
}

std::string all_caps(std::string strvar)
{
	std::string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	std::string lower = "abcdefghijklmnopqrstuvwxyz";
	std::string all_caps = "";
	std::string letter;
	unsigned int k;
	unsigned int i = 0;
	do
		{
		k = 0;
		letter = "no";
		do
			{
			if(strvar.substr(i,1) == lower.substr(k,1))
				{
				letter = "yes";
				all_caps += upper.substr(k,1);
				}
			k++;
			}while(letter == "no" && k < 26);
		if(letter == "no")
			all_caps += strvar.substr(i,1);
		i++;
		}while (i < strvar.length());
	return all_caps;
}

std::string all_lower(std::string strvar)
{
	std::string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	std::string lower = "abcdefghijklmnopqrstuvwxyz";
	std::string all_lower = "";
	std::string letter;
	unsigned int k;
	unsigned int i = 0;
	do
	{
		k = 0;
		letter = "no";
		do
		{
			if(strvar.substr(i,1) == upper.substr(k,1))
			{
				letter = "yes";
				all_lower += lower.substr(k,1);
			}
		++k;
		} while(letter == "no" && k < 26);
		
		if (letter == "no")
			all_lower += strvar.substr(i,1);
		++i;
	} while (i < strvar.length());
	return all_lower;
}
std::string init_cap(std::string strvar)
{
	std::string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	std::string lower = "abcdefghijklmnopqrstuvwxyz";
	std::string init_cap = "";
	std::string letter;
	unsigned int k;
	unsigned int i = 1;

    k = 0;
    letter = "no";
    do
    { 
		if(strvar.substr(0,1) == lower.substr(k,1))
		{
			letter = "yes";
			init_cap += upper.substr(k,1);
		}
		k++;
    } while(letter == "no" && k < 26);
    
	if(letter == "no")
        init_cap += strvar.substr(0,1);

	do
    {
		k = 0;
		letter = "no";
		do
		{ 
			if(strvar.substr(i,1) == upper.substr(k,1))
			{
				letter = "yes";
				init_cap += lower.substr(k,1);
			}
			++k;
			} while(letter == "no" && k < 26);
		if(letter == "no")
			init_cap += strvar.substr(i,1);
		++i;
    } while(i < strvar.length());
return init_cap;
}


std::string replace_comma(std::string strvar)
{
	std::string replace = "";
	for(unsigned int i = 0; i < strvar.length(); ++i)
	{
		if (strvar.substr(i, 1) == ",")
			replace += "-";
		else
			replace += strvar.substr(i, 1);
	}
	return replace;
}

std::string date_format(std::string year_str, std::string date_str)
{
	std::string date_form = "";
	if (date_str.length() == 3)
		date_str = " " + date_str;

	date_form = date_str.substr(0,2) + "/";
	
	if (date_str.substr(2,1) == "0")
		date_form += " " + date_str.substr(3,1);
	else
		date_form += date_str.substr(2,2);

	date_form += "/" + year_str.substr(2,2);
	return date_form;
}

std::string int_to_string_date_format (int year_int, int date_int)
{
	std::stringstream out;
	std::string date_str;
	std::string year_str;

	out << year_int;
	out.flush();
	year_str = out.str();
	out.str(std::string());

	out << date_int;
	out.flush();
	date_str = out.str();

	if (date_str.length() == 3)
		date_str = " " + date_str;
	std::string date_form = date_str.substr(0,2) + "/";
	if (date_str.substr(2,1) == "0")
		date_form += " " + date_str.substr(3,1);
	else
		date_form += date_str.substr(2,2);
	date_form += "/" + year_str.substr(2,2) + '\0';
	return date_form;
}

std::string int_to_string (int integer)
{
	std::stringstream out;
	std::string integer_str;
	
	out << integer;
	out.flush();
	integer_str = out.str();
	return integer_str;
}

}